package MeteoCal.manager;

import MeteoCal.entity.Event;
import MeteoCal.entity.Invitation;
import MeteoCal.entity.Notification;
import MeteoCal.entity.Receivednotification;
import MeteoCal.entity.ReceivednotificationPK;
import MeteoCal.utility.Mail;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Guttadoro, Iommazzo, Saullo
 */
@Stateless
public class NotificationManager {

    @PersistenceContext
    private EntityManager em;
    
     public void edit(Notification entity) {
        em.merge(entity);
        
    }
    
    public void editRn(Receivednotification entity) {
        em.merge(entity);
        em.flush();
    }

    public void remove(Notification entity) {
        em.remove(em.merge(entity));
    }
    
    public void createNotificationWeather(Event event) {
        Collection<Invitation> invitations = event.getInvitationCollection();
        Notification notification=new Notification();
        notification.setIdEvent(event);
        notification.setDate(new Date());
        notification.setText(Notification.PERIODICALLY_UPDATE);
        notification.setClosedToEvent(null);
        em.persist(notification);
        
        Collection<Receivednotification> receivedNotification = new ArrayList<>();
        
        Receivednotification rn = new Receivednotification();
        rn.setNotification(notification);
        rn.setReadn(false);
            
        ReceivednotificationPK rnPK = new ReceivednotificationPK();
        rnPK.setIdNotification(notification.getIdNotification());
        
        
         String objmail = "Periodically update"; // jom
        String textmail = "The weather in the event "+event.getName() +" is changed,<br><br>"+
                                  "<br><br>"+"So, you can login into MeteoCal and check all changes."+"<br><br>"+
                                  "Enjoy yourself :)"+"<br>"+
                                  "<br>Many thanks,<br><i>MeteoCal Staff</i><br><br><br><font size=\"1\" color=\"gray\">"+
                                  "This mail has been sent automatically, so please do not reply to this address</font>";

        if (invitations != null){
             for (Invitation i : invitations){  
                if(i.getResponse().contains("yes")){
                    rnPK.setIdUser(i.getUser().getEmail());
                    Mail.sendMail(i.getUser().getEmail(), "meteocalstaff@gmail.com", objmail, textmail);

                    rn.setReceivednotificationPK(rnPK);
                    rn.setUser(i.getUser());
                }
                receivedNotification.add(rn);
            }
        }
        rnPK.setIdUser(event.getIdCreator().getEmail());
        Mail.sendMail(event.getIdCreator().getEmail(), "meteocalstaff@gmail.com", objmail, textmail); // jom
        rn.setReceivednotificationPK(rnPK);
        rn.setUser(event.getIdCreator());
        receivedNotification.add(rn);
        
        notification.setReceivednotificationCollection(receivedNotification);
        em.merge(notification);
    }
    
    public void saveNotElPart(Notification n) {
        em.persist(n);
        em.flush();
    }

    public void upadateNotification(Notification n) {
        em.merge(n);
    }
    
    
    public void createOneDayNotification(Event event) {
        Collection<Invitation> invitations = event.getInvitationCollection();
        Notification newOneDay = new Notification();
        newOneDay.setIdEvent(event);
        newOneDay.setDate(new Date());
        newOneDay.setText(Notification.ONE_DAY_BEFORE);        
        em.persist(newOneDay);
        
        Collection<Receivednotification> receivedNotification = new ArrayList<>();
        
        Receivednotification rn = new Receivednotification();
        rn.setNotification(newOneDay);
        rn.setReadn(false);
            
        ReceivednotificationPK rnPK = new ReceivednotificationPK();
        rnPK.setIdNotification(newOneDay.getIdNotification());

        
        String objmail = "One day before"; // jom
        String textmail = "The weather in the event "+event.getName() +" is changed and there are bad weather condition in this outdoor event,<br><br>"+
                                  "<br><br>"+"So, you can login into MeteoCal and check all changes."+"<br><br>"+
                                  "Enjoy yourself :)"+"<br>"+
                                  "<br>Many thanks,<br><i>MeteoCal Staff</i><br><br><br><font size=\"1\" color=\"gray\">"+
                                  "This mail has been sent automatically, so please do not reply to this address</font>";

        if (invitations != null){
            for (Invitation i : invitations){ 
            if(i.getResponse().contains("yes")){
                rnPK.setIdUser(i.getUser().getEmail());
                    Mail.sendMail(i.getUser().getEmail(), "meteocalstaff@gmail.com", objmail, textmail);

                rn.setReceivednotificationPK(rnPK);
                rn.setUser(i.getUser());
            }
            receivedNotification.add(rn);
            }

        }
        rnPK.setIdUser(event.getIdCreator().getEmail());
        Mail.sendMail(event.getIdCreator().getEmail(), "meteocalstaff@gmail.com", objmail, textmail); // jom
        rn.setReceivednotificationPK(rnPK);
        rn.setUser(event.getIdCreator());
        receivedNotification.add(rn);
        
        newOneDay.setReceivednotificationCollection(receivedNotification);
        em.merge(newOneDay);
    }

    public void createThreeDaysNotification(Event e, Date suggestedDate) {
        Notification newThreeDays = new Notification();
        newThreeDays.setIdEvent(e);
        newThreeDays.setDate(new Date());
        newThreeDays.setText(Notification.THREE_DAYS_BEFORE);
        newThreeDays.setClosedToEvent(suggestedDate);
        em.persist(newThreeDays);
        
        Collection<Receivednotification> receivedNotification = new ArrayList<>();
        
        Receivednotification rn = new Receivednotification();
        rn.setNotification(newThreeDays);
        rn.setReadn(false);
            
        ReceivednotificationPK rnPK = new ReceivednotificationPK();
        rnPK.setIdNotification(newThreeDays.getIdNotification());

        String objmail = "Three days before"; // jom
        String textmail = "The weather in the event "+e.getName() +" is changed and there are bad weather conditions. We advise you to change the date of the event. We suggest you the: " + suggestedDate + "<br><br>"+
                                  "<br><br>"+"So, you can login into MeteoCal and check all changes."+"<br><br>"+
                                  "Enjoy yourself :)"+"<br>"+
                                  "<br>Many thanks,<br><i>MeteoCal Staff</i><br><br><br><font size=\"1\" color=\"gray\">"+
                                  "This mail has been sent automatically, so please do not reply to this address</font>";

     
        rnPK.setIdUser(e.getIdCreator().getEmail());
        Mail.sendMail(e.getIdCreator().getEmail(), "meteocalstaff@gmail.com", objmail, textmail); // jom

        rn.setReceivednotificationPK(rnPK);
        rn.setUser(e.getIdCreator());
           
        receivedNotification.add(rn);

        newThreeDays.setReceivednotificationCollection(receivedNotification);
        em.merge(newThreeDays);        
    }
     
    public List<Receivednotification> findNotRead(String email){
        List<Receivednotification> notifications = em.createNamedQuery(Receivednotification.findByReadnAndUser, Receivednotification.class).setParameter("readn", false).setParameter("idUser", email).getResultList();
        return notifications;
    }
    
     public List<Receivednotification> findRead(String email){
        List<Receivednotification> notifications = em.createNamedQuery(Receivednotification.findByReadnAndUser, Receivednotification.class).setParameter("readn", true).setParameter("idUser", email).getResultList();
        return notifications;
    }
     
      public Receivednotification findByIdAndUSer(Integer IdNotification, String IdUser){
        Receivednotification notification = em.createNamedQuery(Receivednotification.findByIdNotificationAndUser, Receivednotification.class).setParameter("idNotification", IdNotification).setParameter("idUser", IdUser).getSingleResult();        
        return notification;
    }
}
